<template> 
  <div class="top-bar">
    <div class="top-bar-controls">
      <a-dropdown trigger="click">
        <a class="control-button">
          <MoreOutlined />
        </a>
        <template #overlay>
          <a-menu>
            <a-menu-item key="share" @click="shareApplication">
              分享应用
            </a-menu-item>
            <a-menu-item key="about" @click="openAbout"> 关于我们 </a-menu-item>
          </a-menu>
        </template>
      </a-dropdown>

      <a @click="camemin" class="control-button">
        <MinusOutlined />
      </a>

      <a @click="camemax" class="control-button">
        <ExpandOutlined v-if="!isMaximized" />
        <CompressOutlined v-else />
      </a>

      <a @click="closeWindow" class="control-button close-button">
        <CloseOutlined />
      </a>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import { message } from "ant-design-vue";
import {
  MinusOutlined,
  ExpandOutlined,
  CompressOutlined,
  CloseOutlined,
  MoreOutlined,
} from "@ant-design/icons-vue";
import { Dropdown, Menu } from "ant-design-vue";
const { ipcRenderer } = window.electron || {};

const isMaximized = ref(false);

const closeWindow = () => {
  electronAPI.closeWindow();
};

const camemax = () => {
  electronAPI.toggleFullscreen();
};

const camemin = () => {
  electronAPI.minimizeWindow();
};

const shareApplication = () => {
  const link = `http://49.233.250.28/download.html`;
  navigator.clipboard
    .writeText(link)
    .then(() => {
      message.success("已成功复制到剪贴板，可粘贴后分享给好友");
    })
    .catch((err) => {
      console.error("复制失败", err);
      message.error("复制失败，请重试");
    });
};

const openAbout = () => {
  ipcRenderer.openExternal("http://49.233.250.28/");
};

const handleWindowMaximized = () => {
  isMaximized.value = true;
};

const handleWindowUnmaximized = () => {
  isMaximized.value = false;
};

onMounted(() => {
  electronAPI.onWindowMaximized(handleWindowMaximized);
  electronAPI.onWindowUnmaximized(handleWindowUnmaximized);
});
</script>

<style scoped>
.top-bar {
  width: 100%;
  height: 24px;
  -webkit-app-region: drag;
  background: transparent;
  position: relative;
}

.top-bar-controls {
  position: absolute;
  top: 0;
  right: 0;
  display: flex;
  align-items: center;
  height: 100%;
}

.control-button {
  width: 24px;
  height: 24px;
  padding-left: 24px;
  padding-right: 24px;
  line-height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #4a4a4a;
  background: transparent;
  transition: background-color 0.2s;
  box-sizing: border-box;
  -webkit-app-region: no-drag;
}

.control-button:hover {
  background-color: #f0f0f0;
}

.close-button:hover {
  background-color: #ff4d4f;
}
</style>
